﻿#include "unrealircd.h"


#define DB_COMMADN_PARAM_INT_TO_STRING(index,s) \
if(s){ \
		command_value.paramValues[index] = db_int_to_string(*(s)); \
	}

#define DB_COMMADN_PARAM_STRING_TO_STRING(index,s) \
if(s){ \
		safe_strdup(command_value.paramValues[index],(s));\
	}

#define DB_COMMADN_PARAM_TIMESTAMP_TO_STRING(index,s) \
if(s){ \
		command_value.paramValues[index] = safe_alloc(sizeof(char)*50); \
		db_timestamp_to_string(s,command_value.paramValues[index]); \
	}



/*
此模块存放数据库扩展出去的增删改查函数

*/


D_User * queryImUserById(int id, int *datalen);
D_User * insertImUser(D_User *user, int *status, char *error_message);
D_User * queryImUserByUserName(char *user_name, int *datalen);




/*
@todo:根据id查询用户信息
@params:id为查询用户的id
@params:datalen为查询D_User结构体数组的大小
@return:datalen为-1时表示查询失败，并且返回值为NULL
*/
D_User * queryImUserById(int id, int *datalen) {
	DB_Command_Value command_value;
	command_value.nParams = db_query_imUser_by_id.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_query_imUser_by_id.stmtName;
	//command_value.paramValues[0] = malloc(sizeof(char*)*(command_value.nParams));
	char num[25] = "";
	snprintf(num, 25, "%d", id);
	safe_strdup(command_value.paramValues[0],num);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result != -1) {
		*datalen = result_data.datalen;

		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
	}
}
D_User * queryImUserByUserName(char *user_name, int *datalen) {
	DB_Command_Value command_value;
	command_value.nParams = db_query_imUser_by_user_name.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_query_imUser_by_user_name.stmtName;
	//command_value.paramValues[0] = malloc(sizeof(char*)*(command_value.nParams));
	safe_strdup(command_value.paramValues[0],user_name);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result != -1) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
	}
}

/*
todo:插入一条新的用户信息
param: user为准备插入用户的数据
param: status为插入语句的执行是否成功，0为成功，非0为失败
param: error_message为失败原因
return: 返回刚刚插入的用户信息，包括自动生成的id，如果插入失败则返回NULL
*/
D_User * insertImUser(D_User *user, int *status, char *error_message) {
	*status =0;
	if (!(user->user_name)) {
		*status = -1;
		strcpy(error_message, "缺少用户名");
		return NULL;
	}
	// if(!(user->git_id)){
	// 	*status = -1;
	// 	strcpy(error_message, "缺少gitlib的id");
	// 	return NULL;
	// }
	if (!(user->user_password)) {
		*status = -1;
		strcpy(error_message, "缺少密码");
		return NULL;
	}
	if (!(user->created_at)) {
		*status = -1;
		strcpy(error_message, "缺少创建时间");
		return NULL;
	}
	if (!(user->user_nick)) {
		*status = -1;
		strcpy(error_message, "缺少用户昵称");
		return NULL;
	}
	int UserNameExitNum = 0;
	D_User *u = queryImUserByUserName(user->user_name, &UserNameExitNum);
	db_D_UserFree(u,UserNameExitNum);
	if (UserNameExitNum > 0) {
		*status = -1;
		strcpy(error_message, "用户名已存在");
		return NULL;
	}
	DB_Command_Value command_value;
	for (int i = 0; i < db_insert_imUser.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	command_value.nParams = db_insert_imUser.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_insert_imUser.stmtName;
	if(user->git_id){
		command_value.paramValues[0] = db_int_to_string(*(user->git_id));
	}
	if (user->user_name) {
		safe_strdup(command_value.paramValues[1],user->user_name);
	}
	if (user->user_password){
		safe_strdup(command_value.paramValues[2],user->user_password);
	}
	if (user->tel) {
		safe_strdup(command_value.paramValues[3],user->tel);
	}
	if (user->sex) {
		command_value.paramValues[4] = db_int_to_string(*(user->sex));

	}
	if (user->created_at) {
		char s_time[30];
		db_timestamp_to_string(user->created_at, s_time);
		safe_strdup(command_value.paramValues[5],s_time);
	}
	if (user->user_nick) {
		safe_strdup(command_value.paramValues[6],user->user_nick);
	}
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*status = result_data.datalen;
		return result_data.data;
	}
	else {
		*status = -1;
		strcpy(error_message, "数据库插入数据失败");
		return NULL;
		
	}

}


D_User * queryImUserByEmail(char *email, int *datalen){
	int userEmailLen = 0;
	D_im_user_email * userEmail = queryImUserEmailByEmail(email,&userEmailLen);
	D_User * user=NULL;
	*datalen = userEmailLen;
	if(userEmailLen>0){
		user = queryImUserById(*(userEmail->id), datalen);
	}
	return user;
}

/*
@todo:根据配置名称查询配置值
@params:configure_name为查询配置的配置名称
@params:datalen为查询D_User结构体数组的大小
@return:datalen为-1时表示查询失败，并且返回值为NULL
*/
D_configure * queryImConfigureByName(char *configure_name, int *datalen){
	DB_Command_Value command_value;
	command_value.nParams = db_query_imConfigure_by_configur_name.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_query_imConfigure_by_configur_name.stmtName;
	//command_value.paramValues[0] = malloc(sizeof(char*)*(command_value.nParams));
	command_value.paramValues[0]=NULL;
	safe_strdup(command_value.paramValues[0],configure_name);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result != -1) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
	}
}

/*
@todo:根据openid和thirdid查询第三方登录信息
@params:openid为查询配置的配置名称
@params:thirdid为查询D_User结构体数组的大小
@return:datalen为-1时表示查询失败，并且返回值为NULL
*/
D_third_login *queryImThirdLoginByOpenidAndThirdid(char *openid,int thirdId,int *datalen){
	DB_Command_Value command_value;
	command_value.nParams = db_query_im_third_login_by_openid_and_thirdid.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_query_im_third_login_by_openid_and_thirdid.stmtName;
	//command_value.paramValues[0] = malloc(sizeof(char*)*(command_value.nParams));
	command_value.paramValues[0]=NULL;
	command_value.paramValues[1]=NULL;
	safe_strdup(command_value.paramValues[0],openid);
	char *temp=db_int_to_string(thirdId);
	safe_strdup(command_value.paramValues[1],temp);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result != -1) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
	}
}

D_third_login *queryImThirdLoginByUseridAndThirdid(int userid,int thirdId,int *datalen){
	DB_Command_Value command_value;
	command_value.nParams = db_query_im_third_login_by_userid_and_thirdid.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_query_im_third_login_by_userid_and_thirdid.stmtName;
	//command_value.paramValues[0] = malloc(sizeof(char*)*(command_value.nParams));
	command_value.paramValues[0]=db_int_to_string(userid);
	command_value.paramValues[1]=db_int_to_string(thirdId);

	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result != -1) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
	}
}

D_third_login *queryImThirdLoginByUserid(int userid,int *datalen){
	DB_Command_Value command_value;
	command_value.nParams = db_query_im_third_login_by_userid.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_query_im_third_login_by_userid.stmtName;
	//command_value.paramValues[0] = malloc(sizeof(char*)*(command_value.nParams));
	command_value.paramValues[0]=db_int_to_string(userid);

	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result != -1) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
	}
}


D_third_login *insertImThirdLogin(D_third_login *third_data, int *status, char *error_message){
	*status = 0;
	if(!(third_data->open_id)){
		*status = -1;
		strcpy(error_message, "缺少openid");
		return NULL;
	}
	if(!(third_data->third_id)){
		*status = -1;
		strcpy(error_message, "缺少第三方标识");
		return NULL;
	}
	if(!(third_data->user_id)){
		*status = -1;
		strcpy(error_message, "缺少用户id");
		return NULL;
	}
	int datalen=0;
	queryImThirdLoginByUseridAndThirdid(*(third_data->user_id),*(third_data->third_id),&datalen);
	if(datalen>0){
		*status = -1;
		strcpy(error_message, "该用户已绑定当前第三方用户");
		return NULL;
	}
	else if(datalen<0){
		*status = -1;
		strcpy(error_message, "数据库错误");
		return NULL;
	}
	datalen=0;
	queryImThirdLoginByOpenidAndThirdid(third_data->open_id,*(third_data->third_id),&datalen);
	if(datalen>0){
		*status = -1;
		strcpy(error_message, "该第三方用户已被绑定");
		return NULL;
	}
	else if(datalen<0){
		*status = -1;
		strcpy(error_message, "数据库错误");
		return NULL;
	}
	DB_Command_Value command_value;
	for (int i = 0; i < db_insert_imUser.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	command_value.nParams = db_insert_im_third_login.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_insert_im_third_login.stmtName;

	command_value.paramValues[0] = db_int_to_string(*(third_data->third_id));
	safe_strdup(command_value.paramValues[1],third_data->open_id);
	if (third_data->nick_name) {
		safe_strdup(command_value.paramValues[2],third_data->nick_name);
	}
	command_value.paramValues[3]=db_int_to_string(*(third_data->user_id));
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*status = result_data.datalen;
		return result_data.data;
	}
	else {
		*status = -1;
		strcpy(error_message, "数据库插入数据失败");
		return NULL;
		
	}
}

D_third_login *deleteImThirdLoginByUseridAndThirdId(int userId,int thirdId,int *datalen){
	DB_Command_Value command_value;
	command_value.nParams = db_delete_im_third_login_by_userIdAndThirdId.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_delete_im_third_login_by_userIdAndThirdId.stmtName;
	//command_value.paramValues[0] = malloc(sizeof(char*)*(command_value.nParams));
	command_value.paramValues[0]=db_int_to_string(userId);
	command_value.paramValues[1]=db_int_to_string(thirdId);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result != -1) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
	}
}

char * queryImConfigureValue(char *configure_name){
	int len=0;
	D_configure *configure=queryImConfigureByName(configure_name,&len);
	char *result=NULL;
	if(len==1){
		safe_strdup(result,configure->configure_value);
	}
	db_D_configure_free(configure,len);

	return result;
}

D_User * queryImUserByTel(char *tel, int *datalen) {
	DB_Command_Value command_value;
	command_value.nParams = db_query_imUser_by_tel.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_query_imUser_by_tel.stmtName;
	//command_value.paramValues[0] = malloc(sizeof(char*)*(command_value.nParams));
	safe_strdup(command_value.paramValues[0],tel);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result != -1) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
	}
}




/**
 * 向im_sms插入一条数据
 */
D_im_sms * insertImSms(D_im_sms *sms_data, int *status, char *error_message){
	*status = 0;
	if(!(sms_data->code)){
		*status = -1;
		strcpy(error_message, "缺少验证码");
		return NULL;
	}

	if(!(sms_data->contactvalue)){
		*status = -1;
		strcpy(error_message, "缺少联系方式");
		return NULL;
	}
	if(!(sms_data->sms_channel)){
		*status = -1;
		strcpy(error_message, "缺少联系标志");
		return NULL;
	}
	if(!(sms_data->sms_type)){
		*status = -1;
		strcpy(error_message, "缺少验证类型");
		return NULL;
	}
	if(!(sms_data->valid_time)){
		*status = -1;
		strcpy(error_message, "缺少验证时间");
		return NULL;
	}
	DB_Command_Value command_value;
	for (int i = 0; i < db_insert_im_sms.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	command_value.nParams = db_insert_im_sms.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_insert_im_sms.stmtName;
	if(sms_data->contactvalue){
		safe_strdup(command_value.paramValues[0],sms_data->contactvalue);
	}
	if(sms_data->sms_type){
		command_value.paramValues[1] = db_int_to_string(*(sms_data->sms_type));
	}
	if(sms_data->used){
		command_value.paramValues[2] = db_int_to_string(*(sms_data->used));
	}
	if(sms_data->code){
		safe_strdup(command_value.paramValues[3],sms_data->code);
	}
	if(sms_data->valid_time){
		command_value.paramValues[4] = safe_alloc(sizeof(char)*50);
		db_timestamp_to_string(sms_data->valid_time,command_value.paramValues[4]);
	}
	if(sms_data->sms_channel){
		command_value.paramValues[5] = db_int_to_string(*(sms_data->sms_channel));
	}

	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*status = result_data.datalen;
		return result_data.data;
	}
	else {
		*status = -1;
		strcpy(error_message, "数据库插入数据失败");
		return NULL;
		
	}
}

//根据发送方式和验证类型和验证时间查询im_sms表
D_im_sms * queryImSMSByValueAndTypeAndTime(char *contactvalue,int sms_type,int channel,D_TimeStamp* beginTime,D_TimeStamp* endTime, int *datalen) {
	DB_Command_Value command_value;
	command_value.nParams = db_query_im_sms_by_type_and_contactvalue.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_query_im_sms_by_type_and_contactvalue.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	command_value.paramValues[0] = int_to_string(sms_type);
	safe_strdup(command_value.paramValues[1],contactvalue);
	command_value.paramValues[2] = int_to_string(channel);
	command_value.paramValues[3] = safe_alloc(sizeof(char)*50);
	db_timestamp_to_string(beginTime,command_value.paramValues[3]);
	command_value.paramValues[4] = safe_alloc(sizeof(char)*50);
	db_timestamp_to_string(endTime,command_value.paramValues[4]);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result != -1) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
	}
}

//根据发送方式和验证类型和当前时间来查询im_sms表
D_im_sms * queryImSMSByValueAndType(char *contactvalue,int sms_type,int channel,int min,int *datalen){
	D_TimeStamp *beginTime=safe_alloc(sizeof(D_TimeStamp));
	D_TimeStamp *endTime=safe_alloc(sizeof(D_TimeStamp));
	D_getCurrectTime(endTime);
	time_t timep=time(NULL)-(min*60);
	D_getTimeByTime_t(beginTime,&timep);
	return queryImSMSByValueAndTypeAndTime(contactvalue,sms_type,channel,beginTime,endTime,datalen);
}

//根据identity_no更新im_sms记录
D_im_sms *updataImSMS(D_im_sms *sms_data,int *status, char *error_message){
	*status = 0;
	if(!(sms_data->identity_no)){
		*status = -1;
		strcpy(error_message, "缺少验证编号");
		return NULL;
	}
	if(!(sms_data->code)){
		*status = -1;
		strcpy(error_message, "缺少验证码");
		return NULL;
	}

	if(!(sms_data->contactvalue)){
		*status = -1;
		strcpy(error_message, "缺少联系方式");
		return NULL;
	}
	if(!(sms_data->sms_channel)){
		*status = -1;
		strcpy(error_message, "缺少联系标志");
		return NULL;
	}
	if(!(sms_data->sms_type)){
		*status = -1;
		strcpy(error_message, "缺少验证类型");
		return NULL;
	}
	if(!(sms_data->valid_time)){
		*status = -1;
		strcpy(error_message, "缺少验证时间");
		return NULL;
	}
	DB_Command_Value command_value;
	command_value.nParams = db_updata_im_sms_by_identity_no.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_updata_im_sms_by_identity_no.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	command_value.paramValues[0]=db_int_to_string(*(sms_data->identity_no));
	if(sms_data->sms_type){
		command_value.paramValues[1] = db_int_to_string(*(sms_data->sms_type));
	}
	if(sms_data->used){
		command_value.paramValues[2] = db_int_to_string(*(sms_data->used));
	}
	if(sms_data->code){
		safe_strdup(command_value.paramValues[3],sms_data->code);
	}
	if(sms_data->valid_time){
		command_value.paramValues[4] = safe_alloc(sizeof(char)*50);
		db_timestamp_to_string(sms_data->valid_time,command_value.paramValues[4]);
	}
	if(sms_data->contactvalue){
		safe_strdup(command_value.paramValues[5],sms_data->contactvalue);
	}
	if(sms_data->sms_channel){
		command_value.paramValues[6] = db_int_to_string(*(sms_data->sms_channel));
	}

	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*status = result_data.datalen;
		return result_data.data;
	}
	else {
		*status = -1;
		strcpy(error_message, "数据库更新数据失败");
		return NULL;
		
	}
}

//根据id删除用户
D_User *deleteImUserById(int id,int *status, char *error_message){
	* status = 0;
	DB_Command_Value command_value;
	command_value.nParams = db_delete_imUser.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_delete_imUser.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	command_value.paramValues[0]=db_int_to_string(id);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*status = result_data.datalen;
		return result_data.data;
	}
	else {
		*status = -1;
		strcpy(error_message, "database delete data faile");
		return NULL;
		
	}

}

//根据id更新im_user记录
D_User *updataImUser(D_User *user,int *status, char *error_message){
	*status =0;
	if(!(user->id)){
		*status = -1;
		strcpy(error_message, "lack user id");
		return NULL;
	}
	if(!(user->user_name)){
		*status = -1;
		strcpy(error_message, "lack username");
		return NULL;
	}

	if(!(user->user_password)){
		*status = -1;
		strcpy(error_message, "lack user_password");
		return NULL;
	}
	if(!(user->created_at)){
		*status = -1;
		strcpy(error_message, "lack create time");
		return NULL;
	}
	if(!(user->user_nick)){
		*status = -1;
		strcpy(error_message, "lack user nick");
		return NULL;
	}
	DB_Command_Value command_value;
	command_value.nParams = db_update_imUser.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_update_imUser.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	command_value.paramValues[0] = db_int_to_string(*(user->id));
	DB_COMMADN_PARAM_INT_TO_STRING(1,user->git_id);
	DB_COMMADN_PARAM_STRING_TO_STRING(2,user->user_name);
	DB_COMMADN_PARAM_STRING_TO_STRING(3,user->user_password);
	DB_COMMADN_PARAM_STRING_TO_STRING(4,user->tel);
	DB_COMMADN_PARAM_INT_TO_STRING(5,user->sex);
	DB_COMMADN_PARAM_TIMESTAMP_TO_STRING(6,user->created_at);
	DB_COMMADN_PARAM_STRING_TO_STRING(7,user->user_nick);

	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*status = result_data.datalen;
		return result_data.data;
	}
	else {
		*status = -1;
		strcpy(error_message, "database update faile");
		return NULL;
		
	}
}



D_im_user_email * updateImUserEmail(D_im_user_email* userEmail,int *status, char *error_message){
	*status =0;
	if(!(userEmail->id)){
		*status = -1;
		strcpy(error_message, "lack user id");
		return NULL;
	}
	if(!(userEmail->email)){
		*status = -1;
		strcpy(error_message, "lack email");
		return NULL;
	}
	if(!(userEmail->permition)){
		*status = -1;
		strcpy(error_message, "lack permition");
		return NULL;
	}
	DB_Command_Value command_value;
	command_value.nParams = db_update_im_user_email.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_update_im_user_email.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	DB_COMMADN_PARAM_INT_TO_STRING(0,userEmail->id);
	DB_COMMADN_PARAM_STRING_TO_STRING(1,userEmail->email);
	DB_COMMADN_PARAM_INT_TO_STRING(2,userEmail->permition);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*status = result_data.datalen;
		return result_data.data;
	}
	else {
		*status = -1;
		strcpy(error_message, "database update faile");
		return NULL;
		
	}

}

D_im_user_email * insertImUserEmail(D_im_user_email* userEmail,int *status, char *error_message){
	*status = 0;
	if(!(userEmail->id)){
		*status = -1;
		strcpy(error_message, "lack user id");
		return NULL;
	}
	if(!(userEmail->email)){
		*status = -1;
		strcpy(error_message, "lack email");
		return NULL;
	}
	if(!(userEmail->permition)){
		*status = -1;
		strcpy(error_message, "lack permition");
		return NULL;
	}
	DB_Command_Value command_value;
	command_value.nParams = db_insert_im_user_email.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_insert_im_user_email.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	DB_COMMADN_PARAM_INT_TO_STRING(0,userEmail->id);
	DB_COMMADN_PARAM_STRING_TO_STRING(1,userEmail->email);
	DB_COMMADN_PARAM_INT_TO_STRING(2,userEmail->permition);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*status = result_data.datalen;
		return result_data.data;
	}
	else {
		*status = -1;
		strcpy(error_message, "database update faile");
		return NULL;
		
	}
}

D_im_user_email * queryImUserEmailByEmail(char *email, int *datalen){
	DB_Command_Value command_value;
	command_value.nParams = db_query_im_user_email_by_email.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_query_im_user_email_by_email.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	DB_COMMADN_PARAM_STRING_TO_STRING(0,email);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
		
	}
}

D_im_user_email * queryImUserEmailById(int id, int *datalen){
	DB_Command_Value command_value;
	command_value.nParams = db_query_im_user_email_by_id.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_query_im_user_email_by_id.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	DB_COMMADN_PARAM_INT_TO_STRING(0,&id);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
		
	}
}

D_im_user_email * queryImUserEmailByIdAndPermition(int id,int permition, int *datalen){
	DB_Command_Value command_value;
	command_value.nParams = db_query_im_user_email_by_idAndPermition.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_query_im_user_email_by_idAndPermition.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	DB_COMMADN_PARAM_INT_TO_STRING(0,&id);
	DB_COMMADN_PARAM_INT_TO_STRING(1,&permition);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
		
	}
}

D_im_user_email * queryImUserEmailByEmailAndPermition(char *email,int permition, int *datalen){
	DB_Command_Value command_value;
	command_value.nParams = db_query_im_user_email_by_emailAndPermition.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_query_im_user_email_by_emailAndPermition.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	DB_COMMADN_PARAM_STRING_TO_STRING(0,email);
	DB_COMMADN_PARAM_INT_TO_STRING(1,&permition);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
		
	}
}


D_im_user_email * queryImUserEmailByIdAndEmail(int id,char * email, int *datalen){
	DB_Command_Value command_value;
	command_value.nParams = db_query_im_user_email_by_emailAndPermition.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_query_im_user_email_by_emailAndPermition.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	DB_COMMADN_PARAM_INT_TO_STRING(0,&id);
	DB_COMMADN_PARAM_STRING_TO_STRING(1,email);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
		
	}
}

D_im_user_email * deleteImUserEmailByIdAndEmail(int id,char * email, int *datalen){
	DB_Command_Value command_value;
	command_value.nParams = db_delete_im_user_email_by_idAndEmail.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_delete_im_user_email_by_idAndEmail.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	DB_COMMADN_PARAM_INT_TO_STRING(0,&id);
	DB_COMMADN_PARAM_STRING_TO_STRING(1,email);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
		
	}
}

D_im_login_record* insertImLoginRecord(D_im_login_record* data,int *status, char *error_message){
	*status = 0;
	if(!(data->user_id)){
		*status = -1;
		strcpy(error_message, "lack user id");
		return NULL;
	}
	if(!(data->log_time)){
		*status = -1;
		strcpy(error_message, "lack login time");
		return NULL;
	}
	DB_Command_Value command_value;
	command_value.nParams = db_insert_im_login_record.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_insert_im_login_record.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	DB_COMMADN_PARAM_INT_TO_STRING(0,data->user_id);
	DB_COMMADN_PARAM_TIMESTAMP_TO_STRING(1,data->log_time);
	DB_COMMADN_PARAM_STRING_TO_STRING(2,data->login_ip);
	DB_COMMADN_PARAM_STRING_TO_STRING(3,data->login_mac);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*status = result_data.datalen;
		return result_data.data;
	}
	else {
		*status = -1;
		strcpy(error_message, "database insert faile");
		return NULL;
	}
}

int userLoginLog(int userId,char* ip, char* mac){
	D_im_login_record* data = db_createLoginRecord();
	safe_strdup(data->login_ip,ip);
	safe_strdup(data->login_mac,mac);
	data->user_id = safe_alloc(sizeof(int));
	data->log_time = safe_alloc(sizeof(D_TimeStamp));
	*(data->user_id)  = userId;
	D_getCurrectTime(data->log_time);
	int status=0;
	char error_message[200]="";
	D_im_login_record* rt = insertImLoginRecord(data,&status,error_message);
	db_D_LoginRecordFree(rt,status);
	db_D_LoginRecordFree(data,1);
	if(!mac){
		mac ="NULL";
	}
	if(!ip){
		ip = "NULL";
	}
	if(status<0){
		ircd_log(LOG_ERROR,"database insert user login record , user_id=%d,ip=%s,mac=%s",userId,ip,mac);
		return -1;
	}
	return 0;
}

D_im_relation_applicant * InsertImRelationApplicant(D_im_relation_applicant* data, int *datalen){
	DB_Command_Value command_value;
	command_value.nParams = db_insert_im_relation_applicant.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_insert_im_relation_applicant.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	DB_COMMADN_PARAM_INT_TO_STRING(0,data->applicant_type);
	DB_COMMADN_PARAM_INT_TO_STRING(1,data->applicant_id);
	DB_COMMADN_PARAM_INT_TO_STRING(2,data->respondent_id);
	DB_COMMADN_PARAM_INT_TO_STRING(3,data->project_group_id);
	DB_COMMADN_PARAM_INT_TO_STRING(4,data->applicant_status);
	DB_COMMADN_PARAM_TIMESTAMP_TO_STRING(5,data->applicant_time);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
		
	}
}

D_im_relation_applicant * UpdataImRelationApplicant(D_im_relation_applicant* data, int *datalen){
	DB_Command_Value command_value;
	command_value.nParams = db_update_im_relation_applicant.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_update_im_relation_applicant.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	DB_COMMADN_PARAM_INT_TO_STRING(0,data->relation_applicant_id);
	DB_COMMADN_PARAM_INT_TO_STRING(1,data->applicant_type);
	DB_COMMADN_PARAM_INT_TO_STRING(2,data->applicant_id);
	DB_COMMADN_PARAM_INT_TO_STRING(3,data->respondent_id);
	DB_COMMADN_PARAM_INT_TO_STRING(4,data->project_group_id);
	DB_COMMADN_PARAM_INT_TO_STRING(5,data->applicant_status);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
		
	}
}

D_im_relation_applicant * queryImRelationApplicantByRespondentId(int id, int *datalen){
	DB_Command_Value command_value;
	command_value.nParams = db_query_im_relation_applicant_by_respondentId.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_query_im_relation_applicant_by_respondentId.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	D_TimeStamp creatTime,endTime;
	time_t t = time(NULL);
	D_getTimeByTime_t(&endTime,&t);
	t-= 31 * 24 * 3600;
	D_getTimeByTime_t(&creatTime,&t);
	DB_COMMADN_PARAM_INT_TO_STRING(0,&id);
	DB_COMMADN_PARAM_TIMESTAMP_TO_STRING(1,&creatTime);
	DB_COMMADN_PARAM_TIMESTAMP_TO_STRING(2,&endTime);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
		
	}
}

D_im_relation_applicant * queryImRelationApplicantByApplicantId(int id, int *datalen){
	DB_Command_Value command_value;
	command_value.nParams = db_query_im_relation_applicant_by_applicantId.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_query_im_relation_applicant_by_applicantId.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	D_TimeStamp creatTime,endTime;
	time_t t = time(NULL);
	D_getTimeByTime_t(&endTime,&t);
	t-= 31 * 24 * 3600;
	D_getTimeByTime_t(&creatTime,&t);
	DB_COMMADN_PARAM_INT_TO_STRING(0,&id);
	DB_COMMADN_PARAM_TIMESTAMP_TO_STRING(1,&creatTime);
	DB_COMMADN_PARAM_TIMESTAMP_TO_STRING(2,&endTime);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
		
	}
}

D_im_relation_applicant * queryImRelationApplicantByRelationApplicantId(int id, int *datalen){
	DB_Command_Value command_value;
	command_value.nParams = db_query_im_relation_applicant_by_relationApplicantId.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_query_im_relation_applicant_by_relationApplicantId.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	D_TimeStamp creatTime,endTime;
	time_t t = time(NULL);
	D_getTimeByTime_t(&endTime,&t);
	t-= 31 * 24 * 3600;
	D_getTimeByTime_t(&creatTime,&t);
	DB_COMMADN_PARAM_INT_TO_STRING(0,&id);
	DB_COMMADN_PARAM_TIMESTAMP_TO_STRING(1,&creatTime);
	DB_COMMADN_PARAM_TIMESTAMP_TO_STRING(2,&endTime);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
		
	}
}

D_im_user_relation * InsertImUserRelation(D_im_user_relation* data, int *datalen){
	DB_Command_Value command_value;
	command_value.nParams = db_insert_im_user_relation.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_insert_im_user_relation.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	DB_COMMADN_PARAM_INT_TO_STRING(0,data->user_id);
	DB_COMMADN_PARAM_INT_TO_STRING(1,data->friend_id);
	DB_COMMADN_PARAM_STRING_TO_STRING(2,data->nick_name);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
		
	}
}

D_im_user_relation * UpdataImUserRelation(D_im_user_relation* data, int *datalen){
	DB_Command_Value command_value;
	command_value.nParams = db_update_im_relation_applicant.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_update_im_relation_applicant.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	DB_COMMADN_PARAM_INT_TO_STRING(0,data->user_id);
	DB_COMMADN_PARAM_INT_TO_STRING(1,data->friend_id);
	DB_COMMADN_PARAM_STRING_TO_STRING(2,data->nick_name);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
		
	}
}

D_im_user_relation * queryImUserRelationByUserIdAndFriendId(int userid,int friendid, int *datalen){
	DB_Command_Value command_value;
	command_value.nParams = db_query_im_user_relation_by_UserIdAndFriendId.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_query_im_user_relation_by_UserIdAndFriendId.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	DB_COMMADN_PARAM_INT_TO_STRING(0,&userid);
	DB_COMMADN_PARAM_INT_TO_STRING(1,&friendid);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
		
	}
}

D_im_user_relation * queryImUserRelationByUserId(int userid, int *datalen){
	DB_Command_Value command_value;
	command_value.nParams = db_query_im_user_relation_by_UserId.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_query_im_user_relation_by_UserId.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	DB_COMMADN_PARAM_INT_TO_STRING(0,&userid);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
		
	}
}

D_User * queryImUserFriendsByUserId(int userid, int *datalen){
	DB_Command_Value command_value;
	command_value.nParams = db_query_imUserFriend_by_id.nParams;
	command_value.paramFormats = NULL;
	command_value.paramLengths = NULL;
	command_value.resultFormat = 0;
	command_value.stmtName = db_query_imUserFriend_by_id.stmtName;
	for (int i = 0; i < command_value.nParams; i++) {
		command_value.paramValues[i] = NULL;
	}
	DB_COMMADN_PARAM_INT_TO_STRING(0,&userid);
	DB_Rsult_Data result_data;
	int result = db_queryByPerpared(command_value, &result_data);
	if (result == 0) {
		*datalen = result_data.datalen;
		return result_data.data;
	}
	else {
		*datalen = -1;
		return NULL;
		
	}
}